package com.hxm.demo.sort;

/**
 * description: add a description
 *
 * @author huangxiaoming
 * @version 1.0.0
 * @date 2022/10/09 19:51:17
 */
public class InsertSort {

    public static void main(String[] args) {
        int[] numbers = new int[]{13, 4, 34, 17, 2, 9};
        sort(numbers);
        for (int number : numbers) {
            System.out.println(number);
        }
    }

    public static int[] sort(int[] numbers) {
        if (numbers.length >= 2) {
            for (int i = 1; i < numbers.length; i++) {
                int x = numbers[i];
                int j = i - 1;
                while (j >= 0 && numbers[j] > x) {
                    numbers[j + 1] = numbers[j];
                    j--;
                }
                numbers[j + 1] = x;
            }
        }
        return numbers;
    }
}
